package org.dftproject.genesis.core.agents.scheduler;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.jobs.Job;

public class AgentSchedulerJob extends Job {

	private static final Log log = LogFactory.getLog(AgentSchedulerJob.class);

	private final AgentScheduler scheduler;
	
	public AgentSchedulerJob(AgentScheduler scheduler) {
		super("Agent scheduler");
		
		this.scheduler = scheduler;
	}

	@Override
	protected IStatus run(IProgressMonitor monitor) {
		while (true) {
			Runnable work = scheduler.getWork();
			if (work != null) {
				try {
					work.run();
				} catch (Throwable e) {
					log.error("Scheduled agent work failed", e);
				}
			} else {
				try {
					Thread.sleep(5000);
				} catch (InterruptedException e) {
				}
			}
		}
	}

}
